<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\HttpController\adminapi;

use App\Services\System\SystemMenusServices;
use EasySwoole\Http\Message\Status;
use EasySwoole\EasySwoole\Config;
use EasySwoole\Validate\Validate;
use function sort_list_tier;

/**
 * Description of Common
 *
 * @author zike
 */
class Common extends AuthController {

    protected function getValidateRule(?string $action): ?Validate {
	switch ($action) {
	    case 'login':
		$validate = new Validate();
//                $validate->addColumn('imgcode')->required();
//                $validate->addColumn('account')->required()->alphaDash();
//                $validate->addColumn('pwd')->required();
		return $validate;
		break;
	}
	return null;
    }

    /**
     * 格式化菜单
     * @return mixed
     */
    public function menusList() {
//	$time = time();
//	$this->response()->setCookie('uuid', $time, $time + 86400);
	$data = SystemMenusServices::getInstance()->getMenusTreeList();
	$this->writeJson(Status::CODE_OK, sort_list_tier($data), 'ok');
    }

    /**
     * 订单图表
     */
    public function orderChart() {
	$ret = json_decode('{
		"yAxis": [],
		"legend": [],
		"xAxis": [],
		"serise": [],
		"pre_cycle": [],
		"cycle": []
	    }');
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');

	$cycle = $this->request->param('cycle') ?: 'thirtyday'; //默认30天
	/** @var StoreOrderServices $orderServices */
	$orderServices = app()->make(StoreOrderServices::class);
	$chartdata = $orderServices->orderCharts($cycle);
	return app('json')->success($chartdata);
    }

    /**
     * 首页头部统计数据
     * @return mixed
     */
    public function homeStatics() {
	$ret = json_decode('{"info": [
		{
		    "today": 0,
		    "yesterday": 0,
		    "today_ratio": 0,
		    "week": 0,
		    "last_week": 0,
		    "week_ratio": 0,
		    "total": "0元",
		    "date": "昨日",
		    "title": "销售额",
		    "total_name": "本月销售额"
		},
		{
		    "today": 0,
		    "yesterday": 0,
		    "today_ratio": 0,
		    "week": 0,
		    "last_week": 0,
		    "week_ratio": 0,
		    "total": "0Pv",
		    "date": "昨日",
		    "title": "用户访问量",
		    "total_name": "本月访问量"
		},
		{
		    "today": 0,
		    "yesterday": 0,
		    "today_ratio": 0,
		    "week": 0,
		    "last_week": 0,
		    "week_ratio": 0,
		    "total": "0单",
		    "date": "昨日",
		    "title": "订单量",
		    "total_name": "本月订单量"
		},
		{
		    "today": 0,
		    "yesterday": 0,
		    "today_ratio": 0,
		    "week": 0,
		    "last_week": 0,
		    "week_ratio": 0,
		    "total": "0人",
		    "date": "昨日",
		    "title": "新增用户",
		    "total_name": "本月新增用户"
		}
	    ]
	}');
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
	/** @var StoreOrderServices $orderServices */
	$orderServices = app()->make(StoreOrderServices::class);
	$info = $orderServices->homeStatics();
	return app('json')->success(compact('info'));
    }

    /**
     * 用户图表
     */
    public function userChart() {
	
	$ret = json_decode('{
		"legend": [
		    "用户数"
		],
		"yAxis": {
		    "maxnum": 0
		},
		"xAxis": [
		    "05-08"
		],
		"series": [
		    0
		],
		"bing_xdata": [
		    "未消费用户",
		    "消费一次用户",
		    "留存客户",
		    "回流客户"
		],
		"bing_data": [
		    {
			"name": "未消费用户",
			"value": 1,
			"itemStyle": {
			    "color": "#5cadff"
			}
		    },
		    {
			"name": "消费一次用户",
			"value": 0,
			"itemStyle": {
			    "color": "#b37feb"
			}
		    },
		    {
			"name": "留存客户",
			"value": 0,
			"itemStyle": {
			    "color": "#19be6b"
			}
		    },
		    {
			"name": "回流客户",
			"value": 0,
			"itemStyle": {
			    "color": "#ff9900"
			}
		    }
		]
	    }');
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
	/** @var UserServices $uServices */
	$uServices = app()->make(UserServices::class);
	$chartdata = $uServices->userChart();
	return app('json')->success($chartdata);
    }

    /**
     * 交易额排行
     * @return mixed
     */
    public function purchaseRanking() {
	$ret = ['list'=>[]];
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
	/** @var StoreProductAttrValueServices $valueServices */
	$valueServices = app()->make(StoreProductAttrValueServices::class);
	$list = $valueServices->purchaseRanking();
	return app('json')->success(compact('list'));
    }

    /**
     * 待办事统计
     * @return mixed
     */
    public function jnotice()
    {
	$ret = ['list'=>[]];
	return $this->writeJson(Status::CODE_OK, $ret, 'ok');
	
    }
    /**
     * 获取logo
     * @return mixed
     */
    public function getLogo()
    {
	return $this->writeJson(Status::CODE_OK, [
            'logo' => Config::getInstance()->getConf('site_logo'),
            'logo_square' => Config::getInstance()->getConf('site_logo_square')
        ], 'ok');
    }

}
